﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Common;
using System.Data;

using OptusMobile.Web.ServerComponents.Common;
using System.Data.EntityClient;
using CommonServiceLibrary.EntityFramework;

namespace OptusMobile.Web.ServerComponents.DAL
{
    public partial class RecommendationSysEntities : global::System.Data.Objects.ObjectContext
    {
        /// <summary>
        /// Implementation for calling the SP P_UserLogin
        /// </summary>
        /// <returns>True/False if authentication successfully/failed</returns>
        public bool UserLogin(string userName, string password)
        {
            bool isAuthenticationSuccessful = false;
            try
            {
                //get the connection object from the DataContext
                using (DbConnection conn = this.Connection)
                {
                    DbCommand cmd = conn.CreateCommand();
                    //command text is of the format of 'ContainerName.Stored Proc Name' 
                    cmd.CommandText = String.Format("{0}.{1}", this.DefaultContainerName, "UserLogin");
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    EFUtil.AddEntityParameter(cmd, "v_Number", DbType.String, userName);
                    EFUtil.AddEntityParameter(cmd, "v_Password", DbType.String, password);

                    if (this.Connection.State != System.Data.ConnectionState.Open)
                    {
                        this.Connection.Open();
                    }

                    var result = cmd.ExecuteScalar();

                    isAuthenticationSuccessful = (int)result == 1;

                }
            }
            catch (Exception ex)
            {
                RecommendationSysDALException exception =
                    new RecommendationSysDALException("DAL error happens when authenticating user", ex);
                throw exception;
            }

            return isAuthenticationSuccessful;
        }

        /// <summary>
        /// Implementation for calling the SP P_AdminLogin
        /// </summary>
        /// <returns>True/False if authentication successfully/failed</returns>
        public bool AdminLogin(string userName, string password)
        {
            bool isAuthenticationSuccessful = false;
            try
            {
                //get the connection object from the DataContext
                using (DbConnection conn = this.Connection)
                {
                    DbCommand cmd = conn.CreateCommand();
                    //command text is of the format of 'ContainerName.Stored Proc Name' 
                    cmd.CommandText = String.Format("{0}.{1}", this.DefaultContainerName, "AdminLogin");
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    EFUtil.AddEntityParameter(cmd, "v_userName", DbType.String, userName);
                    EFUtil.AddEntityParameter(cmd, "v_userPassword", DbType.String, password);

                    if (this.Connection.State != System.Data.ConnectionState.Open)
                    {
                        this.Connection.Open();
                    }

                    var result = cmd.ExecuteScalar();

                    isAuthenticationSuccessful = (int)result == 1;

                }
            }
            catch (Exception ex)
            {
                RecommendationSysDALException exception =
                    new RecommendationSysDALException("DAL error happens when authenticating user", ex);
                throw exception;
            }

            return isAuthenticationSuccessful;
        }

        public string CreateID(string tableName)
        {
            string ID = null;
            try
            {
                //get the connection object from the DataContext
                using (DbConnection conn = this.Connection)
                {
                    DbCommand cmd = conn.CreateCommand();
                    //command text is of the format of 'ContainerName.Stored Proc Name' 
                    cmd.CommandText = String.Format("{0}.{1}", this.DefaultContainerName, "CreateID");
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    EFUtil.AddEntityParameter(cmd, "tableName", DbType.String, tableName);
                    EFUtil.AddEntityParameter(cmd, "time", DbType.Date, DateTime.Now.Date);

                    if (this.Connection.State != System.Data.ConnectionState.Open)
                    {
                        this.Connection.Open();
                    }

                    ID = cmd.ExecuteScalar().ToString();
                }
            }
            catch (Exception ex)
            {
                RecommendationSysDALException exception =
                    new RecommendationSysDALException("DAL error happens when generating ID", ex);
                throw exception;
            }

            return ID;
        }

        /// <summary>
        /// Implementation for calling the SP InsertClickRecord
        /// </summary>
        public void InsertClickRecord(string handsetID)
        {
            try
            {
                //get the connection object from the DataContext
                using (DbConnection conn = this.Connection)
                {
                    DbCommand cmd = conn.CreateCommand();
                    //command text is of the format of 'ContainerName.Stored Proc Name' 
                    cmd.CommandText = String.Format("{0}.{1}", this.DefaultContainerName, "InsertClickRecord");
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    EFUtil.AddEntityParameter(cmd, "handsetID", DbType.String, handsetID);

                    if (this.Connection.State != System.Data.ConnectionState.Open)
                    {
                        this.Connection.Open();
                    }

                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                RecommendationSysDALException exception =
                    new RecommendationSysDALException("DAL error happens when inserting click record", ex);
                throw exception;
            }
        }
    }
}
